package com.lance.answer.question_export;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.lance.answer.gui.answer.AnswerObject;
import com.lance.answer.util.MD5Utils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class AnswerFix2 {

    /**
     * 	"questionCode":"7174AD2EF06A082270F135FA5298BF9D",
     * 		"questionTitle":"个体通过社会化得以（ ），获得发展的基点。",
     * @param args
     */
    public static void main(String[] args) throws IOException {
        fix("人体解剖生理学");
    }

    public static void fix(String subjectName) throws IOException {
        subjectName = subjectName.trim();
        InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(subjectName + "/" + subjectName + "-all.json");
        String answerStr1 = IOUtils.toString(inputStream,"utf-8");
        List<AnswerObject> list1 = JSONObject.parseArray(answerStr1,AnswerObject.class);



        System.out.println(list1.size());
        int i = 0;
        for (AnswerObject answerObject:list1) {
            String questionCode = MD5Utils.encode(subjectName + answerObject.getQuestionTitle().trim());
            answerObject.setQuestionCode(questionCode);
            if (StringUtils.isEmpty(answerObject.getAnswerType())) {
                answerObject.setAnswerType("click");
            }
            LinkedList<String> realAnswers = new LinkedList<>();
            for (String item:answerObject.getAnswers()) {
                if (item.endsWith(" ")) {
                    i++;
                }
               realAnswers.add(item.trim());
            }
            answerObject.setAnswers(realAnswers);
        }

        System.out.println(i);
        String path = "src/main/resources/" + subjectName + "/" + subjectName + "-answer1.json";
        FileUtils.write(new File(path), JSONObject.toJSONString(list1, SerializerFeature.PrettyFormat),"utf-8");
    }
}
